package com.example.wishstar.repository;

import android.os.AsyncTask;
import android.util.Log;

import androidx.lifecycle.LiveData;

import com.example.wishstar.api.Api;
import com.example.wishstar.db.UserDao;
import com.example.wishstar.model.OldHabit;
import com.example.wishstar.model.OldHabitWish;
import com.example.wishstar.model.OldWish;
import com.example.wishstar.utils.ApiResponse;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class OldHabitWishRepository {
    private String TAG = "OldHabitWishRepository";
    private UserDao userDao;
    private Api api;

    public OldHabitWishRepository(UserDao userDao, Api api) {
        this.userDao = userDao;
        this.api = api;
    }

    public LiveData<List<OldHabit>> getOldHabits(int userId) {
        refresh(userId);
        return userDao.getOldHabitByuserId(userId);
    }

    public LiveData<List<OldWish>> getOldWishs(int userId) {
        refresh(userId);
        return userDao.getOldWishByuserId(userId);
    }

    public void refresh(Integer userId) {
        api.getOldHabitWish(userId).enqueue(new Callback<ApiResponse<OldHabitWish>>() {
            @Override
            public void onResponse(Call<ApiResponse<OldHabitWish>> call, Response<ApiResponse<OldHabitWish>> response) {
                if (response.body() != null && response.body().code == 200) {
                    Log.e(TAG, "refresh->response" + response.body());
                    insertOldHabit(response.body().getData().getSavedHabitList());
                    insertOldWish(response.body().getData().getSavedWishList());
                }
            }

            @Override
            public void onFailure(Call<ApiResponse<OldHabitWish>> call, Throwable t) {
                Log.e(TAG, "refresh()->onFailure()");
            }
        });
    }

    public void insertOldHabit(List<OldHabit> oldHabitList) {
        AsyncTask.execute(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < oldHabitList.size(); ++i) {
                    Log.e(TAG, oldHabitList.get(i).toString());

                    OldHabit oldHabit = oldHabitList.get(i);
                    oldHabit.dayNumString = "坚持 " + oldHabit.dayNum + " 天";

                    userDao.insertOldHabit(oldHabit);
                }
            }
        });
    }

    public void insertOldWish(List<OldWish> oldWishList) {
        AsyncTask.execute(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < oldWishList.size(); ++i) {
                    Log.e(TAG, oldWishList.get(i).toString());

                    OldWish oldWish = oldWishList.get(i);
                    oldWish.favourNumString = "获得 " + oldWish.favourNum + " 赞";

                    userDao.insertOldWish(oldWish);
                }
            }
        });
    }

    public void deleteWishByWishId(int wishId) {
        api.deleteWish(wishId).enqueue(new Callback<ApiResponse<String>>() {
            @Override
            public void onResponse(Call<ApiResponse<String>> call, Response<ApiResponse<String>> response) {
                Log.e(TAG, "deleteWish()->onResponse()");
                if(response.body() != null && response.body().code == 200) {
                    deleteOldWish(wishId);
                }
            }

            @Override
            public void onFailure(Call<ApiResponse<String>> call, Throwable t) {
                Log.e(TAG, "deleteWish()->onFailure()");
            }
        });
    }

    private void deleteOldWish(int wishId) {
        AsyncTask.execute(new Runnable() {
            @Override
            public void run() {
                userDao.deleteOldWishByWishId(wishId);
            }
        });
    }

    public void deleteHabitByHabitId(int habitId) {
        api.deleteHabit(habitId).enqueue(new Callback<ApiResponse<String>>() {
            @Override
            public void onResponse(Call<ApiResponse<String>> call, Response<ApiResponse<String>> response) {
                Log.e(TAG, "deleteHabit()->onResponse()");
                if(response.body() != null && response.body().code == 200) {
                    deleteOldHabit(habitId);
                }
            }

            @Override
            public void onFailure(Call<ApiResponse<String>> call, Throwable t) {
                Log.e(TAG, "deleteHabit()->onFailure()");
            }
        });
    }

    private void deleteOldHabit(int habitId) {
        AsyncTask.execute(new Runnable() {
            @Override
            public void run() {
                userDao.deleteOldHabitByHabitId(habitId);
            }
        });
    }
}
